Skip to main content

Caching Strategies for System Design

Overview Comparison

PatternWrites Go ToDB ConsistencyRead FreshnessWrite LatencyData Loss RiskBest For
Write-throughCache → DB (sync)StrongAlways freshHigherLowBanking, orders, inventory
Write-backCache → DB (async)EventualFresh (cache), Stale (DB)LowHigh (cache crash)Social media likes, analytics
Write-aroundDB only (cache on read)StrongMay be staleNormalLowIoT logs, cold data

1. Write-through Caching

Definition

Every write operation first goes to the cache, then synchronously writes to the database. Cache and DB are always in sync.

How It Works

User RequestUpdate CacheUpdate DB (sync)Response

Pros

  • ✅ Strong consistency between cache and DB
  • ✅ No data loss risk on cache eviction/crash
  • ✅ Reads are always fresh
  • ✅ Simplified failure recovery

Cons

  • ❌ Higher write latency (extra step to DB)
  • ❌ Increased DB load (every write propagates immediately)
  • ❌ Wasted writes for rarely-read data

When to Use

  • Systems requiring strong consistency
  • Financial transactions
  • Inventory management
  • Booking systems (flights, hotels, tickets)
  • E-commerce checkout
  • Shopping cart synchronization

Real-World Example: E-Commerce Inventory

User buys product

Update cache (quantity: 10099)

Update DB synchronously

Return success to user

2. Write-back (Write-behind) Caching

Definition

Writes go to the cache only. The cache asynchronously flushes data to the DB later (batched or scheduled).

How It Works

User RequestUpdate CacheResponse (fast)

Async batch write to DB (later)

Pros

  • ✅ Low write latency (fast response)
  • ✅ Can batch updates → fewer DB writes
  • ✅ Better throughput for high-write workloads
  • ✅ Reduced DB load

Cons

  • ❌ Risk of data loss if cache crashes before flush
  • ❌ DB may be eventually consistent
  • ❌ Harder failure recovery
  • ❌ Reads from DB can be stale

When to Use

  • Systems where performance > strict consistency
  • Analytics and metrics
  • Logging systems
  • Recommendation engines
  • Social media counters (likes, views, shares)
  • High write volume with tolerable persistence lag

Real-World Example: Product View Counter

User views product

Increment counter in cache (fast)

Return response immediately

Flush to DB every 5 minutes (batched)

3. Write-around Caching

Definition

Writes go directly to the DB, bypassing the cache. Cache is populated only when data is read later (lazy loading).

How It Works

Write: User RequestUpdate DB only → Response
Read: User RequestCheck CacheMissRead DBPopulate Cache

Pros

  • ✅ Reduces cache pollution
  • ✅ Cache stores only frequently read data
  • ✅ Good when writes are rarely followed by reads
  • ✅ Efficient cache utilization

Cons

  • ❌ First read after write = cache miss → higher latency
  • ❌ Cache may remain stale until next read
  • ❌ Higher DB read load initially

When to Use

  • Write-heavy data that's rarely read
  • Large datasets where caching everything isn't practical
  • IoT sensor data / telemetry
  • Historical logs and archives
  • Cold storage scenarios

Real-World Example: Product Reviews

User submits review

Write directly to DB

Cache not updated

When someone reads reviews → Cache miss → Load from DBCache it

Decision Matrix

RequirementRecommended Pattern
Strong consistency neededWrite-through
High write performance neededWrite-back
Avoid cache pollutionWrite-around
Financial transactionsWrite-through
Analytics/metricsWrite-back
Cold/historical dataWrite-around

E-Commerce System: Hybrid Approach

ComponentStrategyReason
Product InventoryWrite-throughStrong consistency required
Order StatusWrite-throughCritical transactional data
Shopping CartWrite-throughMulti-device consistency
Product ViewsWrite-backHigh volume, eventual consistency OK
User Activity LogsWrite-backPerformance over consistency
Recommendation DataWrite-backCan tolerate lag
Old ReviewsWrite-aroundRarely accessed
Archived OrdersWrite-aroundCold data
Historical SalesWrite-aroundLow read frequency

Interview Talking Points

Quick Rule of Thumb

  • Write-through → Consistency matters most
  • Write-back → Performance matters most
  • Write-around → Avoid cache pollution

Strong Answer Template

"I'd use write-through for critical transactional data like orders and payments where consistency is paramount. For high-throughput non-critical updates like view counts and metrics, I'd use write-back to optimize performance. Finally, write-around would handle data that's written frequently but rarely read, such as logs or cold storage. Often, a hybrid strategy is employed based on access patterns and consistency requirements."


Key Tradeoffs Summary

CriteriaWrite-throughWrite-backWrite-around
ConsistencyStrongEventualStrong
Write SpeedSlowerFasterNormal
Read SpeedFastFastSlow (first read)
Cache FreshnessAlwaysAlwaysOn-demand
Data Loss RiskLowHighLow
DB LoadHighLowMedium
ComplexityLowHighLow

Additional Considerations

Failure Scenarios

Write-through: If cache fails, writes still reach DB (durable)

Write-back: If cache fails before flush, data is lost (requires backup strategies)

Write-around: If DB fails, writes fail (but no cache inconsistency)

Scalability

Write-through: DB becomes bottleneck under high write load

Write-back: Scales well for writes, requires robust async processing

Write-around: Scales based on read patterns, cache stays lean